package pers.mine.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pers.mine.dto.QueryHandler;
import pers.mine.po.GoodsGroup;
import pers.mine.po.GoodsItem;
import pers.mine.service.inter.GoodsGroupService;
import pers.mine.utils.SimpleUtil;
import pers.mine.vo.Info;
import pers.mine.dto.PageInfo;

/**
 * 商品组 Controller
 * 
 * @author Mine
 * @since 2016年11月28日_下午2:52:57
 */
@Controller
@RequestMapping("/goodsGroup")
public class AdminGoodsGroupController {
	private String pageTitle = "商品组管理";
	private Info indexinfo = new Info();// 放到首页的信息
	@Autowired
	GoodsGroupService goodsGroupService;
	String onePath = "admin/goodsgroup/";

	/**
	 * 依据分页信息,关键字信息 获得商品列表,并展示到后台商品页
	 */
	@RequestMapping(value = "/show")
	public String show(Integer nowPage, Integer pageSize, Integer pageCount,
			String keys, HttpServletRequest request) {
		QueryHandler qh = QueryHandler.creat();
		if (nowPage == null) {
			nowPage = 1;
		}
		if (pageSize == null) {
			pageSize = 5;
		}
		if (pageCount != null && nowPage != null && pageCount < nowPage) {
			nowPage = pageCount;
		}
		// 分页设置
		qh.limit(nowPage, pageSize);
		if (keys != null && !"".equals(keys.trim())) {
			// 关键字写回页面
			request.setAttribute("keys", keys);
			// 写死了。。。
			qh.orColumnLike("gg_name", keys)
				.orColumnLike("gg_desc", keys)
				.orColumnLike("gg_id", keys);
		}
		// 排序方式
		qh.OrderByASC("gg_id").set();
		// 依据筛选条件获取
		PageInfo<GoodsGroup> pi = goodsGroupService.findByQueryHandler(qh);
		// System.out.println(pi);
		// 判断是否可以放入信息
		Info temp = SimpleUtil.getIndexInfoWithRequest(request);
		if (temp != null) {
			indexinfo = temp;
		}
		if (indexinfo.isCanChange()) {
			indexinfo.setType(Info.SUCCESS);
			if (keys != null && !"".equals(keys.trim())) {
				indexinfo.setMsg("[商品组显示]:对于关键字[" + keys + "]查询商品组成功...");
			} else {
				indexinfo.setMsg("[商品组显示]:查询所有商品组成功...");
			}
		} else {
			indexinfo.setCanChange(true);
		}
		request.setAttribute("pageTitle", pageTitle);
		request.setAttribute("indexinfo", indexinfo);
		request.setAttribute("goodsGroupPageInfo", pi);
		return onePath + "goodsGroupPage";
	}

	// 添加
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(GoodsGroup goodsGroup, Model model) {
		if (goodsGroup == null) {
			System.out.println("信息有误...[这里和数据校验重复了]");
			return "forward:show";
		}
		// 执行添加操作
		if (goodsGroupService.addGoodsGroup(goodsGroup)) {
			indexinfo.setType(Info.SUCCESS);
			indexinfo.setMsg("[添加商品组]:完成添加商品组操作...");
		} else {
			indexinfo.setType(Info.ERROR);
			indexinfo.setMsg("[添加商品组]:添加商品组失败...");
		}
		indexinfo.setCanChange(false);
		model.addAttribute("indexinfo", indexinfo);
		// 内部转发
		return "forward:show";
	}

	// 获取商品组修改页面
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String getEditGoodsInfoPage(@PathVariable("id") Integer id,
			Model model, HttpServletRequest request) {
		GoodsGroup one = null;
		if (id != null && id > 0) {
			// 获取指定id商品信息
			one = goodsGroupService.findOneById(id);
			if (one != null) {
				// 可能是修改失败转发过来的
				model.addAttribute("oneGoodsGroup", one);
				Info temp = SimpleUtil.getIndexInfoWithRequest(request);
				if (temp != null) {
					model.addAttribute("indexinfo", temp);
				}
				model.addAttribute("pageTitle", pageTitle);
				indexinfo.setCanChange(true);
				return onePath + "editGoodsGroup";
			}
		}
		indexinfo.setType(Info.ERROR);
		indexinfo.setMsg("[商品组信息修改]:非法的编号...");
		indexinfo.setCanChange(false);
		model.addAttribute("indexinfo", indexinfo);
		// 内部转发
		return "forward:show";
	}
	// 执行修改
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String editBaseInfo(GoodsGroup goodsGroup, Model model) {
		if (goodsGroup == null) {
			indexinfo.setType(Info.ERROR);
			indexinfo.setMsg("[商品组信息修改]:未知错误...");
			indexinfo.setCanChange(false);
			model.addAttribute("indexinfo", indexinfo);
			return "forward:show";
		}
		// 执行修改操作
		if (goodsGroupService.editGoodsGroup(goodsGroup)) {// 成功
			indexinfo.setType(Info.SUCCESS);
			indexinfo
					.setMsg("[商品组信息修改]:修改#" + goodsGroup.getId() + "号商品组成功...");
			indexinfo.setCanChange(false);
			model.addAttribute("indexinfo", indexinfo);
			return "forward:show";
		} else {// 失败
			indexinfo.setType(Info.SUCCESS);
			indexinfo.setMsg("[商品组信息修改]:修改#" + goodsGroup.getId()
					+ "号商品组失败,重新修改...");
			indexinfo.setCanChange(false);
			model.addAttribute("indexinfo", indexinfo);
			return "redirect:" + goodsGroup.getId();
		}

	}
}
